package models

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
)

// Field 领域
type Field struct {
	Id int64
	Name string
	Desc string
	FieldNo string
	CreateTime time.Time	`orm:"auto_now_add"`
	UpdateTime time.Time	`orm:"auto_now"`
	IsDelete bool			`orm:"default(0)"`
}

func (field *Field) TableName() string {
	return "t_field"
}

func NewField() *Field {
	return &Field{}
}

// Save 保存
func (field *Field) Save() (int64, error) {
	o := orm.NewOrm()
	return o.Insert(field)
}

// Update 更新指定字段
func (field *Field) Update(cols ...string) (int64, error) {
	o := orm.NewOrm()
	return o.Update(field, cols...)
}

// Delete 删除
func (field *Field) Delete() (int64, error) {
	o := orm.NewOrm()
	return o.Delete(field)
}

// DelLogic 逻辑删除
func (field *Field) DelLogic() error {
	if field.Id < 1 {
		return nil
	}
	o := orm.NewOrm()
	_, err := o.QueryTable(field).Filter(IdColumnName, field.Id).Update(orm.Params{
		"is_delete": 1,
	})
	return err
}

// GetById 根据id获取
func (field *Field) GetById() error {
	o := orm.NewOrm()
	return o.Read(field)
}

//func init() {
//	orm.RegisterModel(new(Field))
//}